package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.MachFileDTO;
import com.whfc.mach.param.MachFileAddParam;
import com.whfc.mach.param.MachFileRenameParam;
import com.whfc.ms.api.mach.service.MsMachFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author sun_guodong
 * @description 设备档案管理
 * @date 2021-04-07
 */
@RestController
@RequestMapping("/ms/api/v1/mach/file")
public class MsMachFileController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachFileService machFileService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("machId") Integer machId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("设备档案列表,loginer:{},machId:{},pageNum:{},pageSize:{}", user.getUsername(), machId, pageNum, pageSize);
        PageData<MachFileDTO> pageData = machFileService.list(machId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody MachFileAddParam param) {
        logger.info("添加设备档案,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setCreateUserId(user.getId());
        param.setCreateUserName(user.getNickname());
        machFileService.add(param);
        return ResultUtil.success();
    }

    @PostMapping("/rename")
    public Result rename(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @Validated @RequestBody MachFileRenameParam param) {
        logger.info("设备档案重命名,loginer:{},param:{}", user.getUsername(), param.toString());
        machFileService.rename(param);
        return ResultUtil.success();
    }

    @PostMapping("/del/{fileId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("fileId") Integer fileId) {
        logger.info("删除设备档案,loginer:{},fileId:{}", user.getUsername(), fileId);
        machFileService.del(fileId);
        return ResultUtil.success();
    }

    @PostMapping("/upload")
    public Result upload(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("file") MultipartFile file) {
        logger.info("上传设备档案,loginer:{}", user.getUsername());
        OssPathDTO pathDTO = machFileService.upload(file);
        return ResultUtil.success(pathDTO);
    }
}
